package com.ly.controller;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.anti.ResponseHeaderModel;
import com.anti.model.AntiAccount;
import com.anti.model.AntiRole;
import com.anti.utils.ResponseModelUtils;
import com.ly.service.RoleCilentService;
import com.ly.service.UserClientService;

import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private RoleCilentService roleCilentService;
	@Autowired
	private UserClientService userClientService;
	
	@RequestMapping("/createUser")
	@GlobalTransactional(name="my_test_tx_group",rollbackFor =Exception.class )
	public ResponseHeaderModel createUser(@RequestBody AntiAccount antiAccount) {
		String xid = GlobalTransactionContext.getCurrentOrCreate().getXid();
		int roleId = RandomUtils.nextInt();		
			AntiRole antiRole = new AntiRole();
			antiRole.setId(roleId);
			antiRole.setName("role"+roleId);
			//antiRole.setCreateBy(xid);
			roleCilentService.createOrder(antiRole);
			antiAccount.setRoleId(roleId);
			//antiAccount.setCreateBy(xid);
			userClientService.add(antiAccount);
		return ResponseModelUtils.success();
	}
}
